package cn.edu.sgu.www.mhxysy.pager;

import cn.edu.sgu.www.mhxysy.base.Sorter;
import cn.edu.sgu.www.mhxysy.dto.DataTransferObject;
import cn.edu.sgu.www.mhxysy.easyui.filter.FilterRule;
import cn.edu.sgu.www.mhxysy.util.CollectionUtils;
import cn.edu.sgu.www.mhxysy.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.List;

/**
 * 分页查询参数实体类
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ApiModel
public class Pager<T> extends Sorter implements DataTransferObject {

    /**
     * 页数
     */
    @ApiModelProperty(value = "页数")
    private Integer page;

    /**
     * 每页的记录数
     */
    @ApiModelProperty(value = "每页的记录数")
    private Integer rows;

    /**
     * 过滤规则
     */
    @ApiModelProperty(value = "过滤规则")
    private List<FilterRule> filterRules;

    /**
     * 根据Pager创建Page对象
     * @param pager Pager
     * @return Page
     */
    public static <T> Page<T> ofPage(Pager<T> pager) {
        return new Page<>(pager.getPage(), pager.getRows());
    }

    /**
     * 根据Pager创建QueryWrapper对象
     * @param pager Pager
     * @return QueryWrapper<T>
     */
    public static <T> QueryWrapper<T> getQueryWrapper(Pager<T> pager) {
        return getQueryWrapper(pager, false);
    }

    /**
     * 根据Pager创建QueryWrapper对象
     * @param pager Pager<T>
     * @param enableSort 是否开启排序
     * @return QueryWrapper<T>
     */
    public static <T> QueryWrapper<T> getQueryWrapper(Pager<T> pager, boolean enableSort) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        List<FilterRule> filterRules = pager.getFilterRules();

        if (CollectionUtils.isNotEmpty(filterRules)) {
            for (FilterRule filterRule : filterRules) {
                String field = StringUtils.toLower(filterRule.getField());
                String value = filterRule.getValue();

                if (StringUtils.isNotEmpty(value)) {
                    switch (filterRule.getOperator()) {
                        case less:
                            queryWrapper.lt(field, value);
                            break;
                        case equal:
                            queryWrapper.eq(field, value);
                            break;
                        case greater:
                            queryWrapper.gt(field, value);
                            break;
                        case notequal:
                            queryWrapper.ne(field, value);
                            break;
                        case lessorequal:
                            queryWrapper.le(field, value);
                            break;
                        case greaterorequal:
                            queryWrapper.ge(field, value);
                            break;
                        case beginwith:
                            queryWrapper.likeLeft(field, value);
                            break;
                        case endwith:
                            queryWrapper.likeRight(field, value);
                            break;
                        case contains:
                            queryWrapper.like(field, value);
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        if (enableSort) {
            // 得到order by语句
            String statement = getOrderByStatement(pager);

            if (StringUtils.isNotEmpty(statement)) {
                queryWrapper.last(statement);
            }
        }

        return queryWrapper;
    }

}